import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * GameBoard
 *
 * @author Jiachi Zhu
 * @date 2024/04/21
 */
public class ScoreManager implements Serializable {
    private Map<Character, Integer> letterScores;
    /**
     * The constant playerScores.
     */
    public static Map<Player, Integer> playerScores = new HashMap<>();

    /**
     * Instantiates a new Score manager.
     */
    public ScoreManager() {
        initializeScores();
    }

    /**
     * Initialise the fraction corresponding to the letter
     */
    private void initializeScores() {
        letterScores = new HashMap<>();
        String letters = "AEIOULNRSTDGBCMPFHVWYKJXQZ";
        int[] scores = {1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 8, 8, 10, 10};
        for (int i = 0; i < letters.length(); i++) {
            letterScores.put(letters.charAt(i), scores[i]);
        }
    }

    /**
     * Calculate word score int.
     *
     * @param word      the word's
     * @param row       the word's row
     * @param col       the word's col
     * @param direction the word's direction
     * @return Score corresponding to the word
     */
    public int calculateWordScore(String word, int row, int col, char direction) {
        int wordScore = 0;
        int wordMultiplier = 1;

        for (int i = 0; i < word.length(); i++) {
            int currentRow = row;
            int currentCol = col;
            if (direction == 'V') {
                currentRow += i;
            } else {
                currentCol += i;
            }

            int letterScore = letterScores.get(word.charAt(i));
            SpecialGrid.TileType type = GameBoard.tileTypes[currentRow][currentCol];

            switch (type) {
                case DOUBLE_LETTER:
                    letterScore *= 2;
                    break;
                case TRIPLE_LETTER:
                    letterScore *= 3;
                    break;
                case DOUBLE_WORD:
                    wordMultiplier *= 2;
                    break;
                case TRIPLE_WORD:
                    wordMultiplier *= 3;
                    break;
                default:

                    break;
            }

            wordScore += letterScore;
        }

        wordScore *= wordMultiplier;
        return wordScore;
    }

}

